using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Data;
using THSERP.BaseProvider;
using THSLib;
using THSERP.BusinessEntities;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;

namespace THSERP.Modules.POS
{
    public partial class GELocationsGridControl : THSGridControl
    {
        public GELocationsGridControl()
        {
            InitializeComponent();
        }

        public GELocationsGridControl(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            DataSet dsAllLocation = new GELocationsController().GetAllObjects();
            List<GELocationsInfo> listGELocations = new List<GELocationsInfo>();
            foreach (DataRow row in dsAllLocation.Tables[0].Rows)
            {
                GELocationsInfo obj = (GELocationsInfo)new GELocationsController().GetObjectFromDataRow(row);
                if (obj.GELocationParentID > 0)
                    listGELocations.Add(obj);
            }
            entity.GELocationsList.Invalidate(listGELocations);
            bds.DataSource = entity.GELocationsList;
            //(((DevExpress.XtraGrid.Views.Grid.GridView)this.MainView).Columns["GELocationParentID"].ColumnEdit).r
            this.DataSource = bds;
        }
        protected override GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            if (gridView.Columns["GELocationName"] != null)
            {
                gridView.Columns["GELocationName"].VisibleIndex = 0;
                gridView.Columns["GELocationName"].Width = 100;
                gridView.Columns["GELocationName"].AppearanceCell.BackColor = Color.WhiteSmoke;

            }
            if (gridView.Columns["GELocationParentID"] != null)
            {
                gridView.Columns["GELocationParentID"].Caption = " ";
                DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit reponsitory = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
                reponsitory.DisplayMember = "GELocationName";
                reponsitory.ValueMember = "GELocationID";
                reponsitory.NullText = "";
                gridView.Columns["GELocationParentID"].ColumnEdit = reponsitory;
                gridView.Columns["GELocationParentID"].Group();
            }
            return gridView;
        }
        protected override void GridView_RowStyle(object sender, RowStyleEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0)
                {
                    DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                    GELocationsInfo objGELocationsInfo = (GELocationsInfo)gridView.GetRow(e.RowHandle);
                    if (objGELocationsInfo != null)
                    {
                        if (objGELocationsInfo.GELocationStatusCombo != "New")
                        {
                            e.Appearance.ForeColor = Color.Red;
                        }
                        else
                            e.Appearance.ForeColor = Color.Blue;
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }
        protected override void OnClick(EventArgs e)
        {
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            if (((POSModule)Screen.Module).Toolbar.IsNullOrNoneAction())
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = entity.GELocationsList.GridView;
                int iRowHandle = gridView.FocusedRowHandle;
                if (iRowHandle >= 0)
                    ((POSModule)Screen.Module).GetNewPOSByCurrentEmployeeAndCurrentDateAndLocation(Convert.ToInt32(gridView.GetRowCellValue(iRowHandle, "GELocationID")));
            }
        }
        protected override void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            if (((POSModule)Screen.Module).Toolbar.IsNullOrNoneAction())
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                int iRowHandle = gridView.FocusedRowHandle;
                if (iRowHandle >= 0)
                    ((POSModule)Screen.Module).GetNewPOSByCurrentEmployeeAndCurrentDateAndLocation(Convert.ToInt32(gridView.GetRowCellValue(iRowHandle, "GELocationID")));
            }
        }
    }

}
